﻿using System;
using System.Collections;
using System.Configuration;
using System.Data;

using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.HtmlControls;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Xml.Linq;
using MacViet.DataBaseAPI;

public partial class Control_Employees_Add_Employee : System.Web.UI.UserControl
{
    private int acc_id = 0;
    private string currentDate = DateTime.Now.Month.ToString() + "/" + DateTime.Now.Day.ToString() + "/" + DateTime.Now.Year.ToString();
    EMPLOYEESDataAPI api = new EMPLOYEESDataAPI();   
    protected void Page_Load(object sender, EventArgs e)
    {
        if (Session["ACC_ID"] != null)
        {
            acc_id = int.Parse(Session["ACC_ID"].ToString());
            tbxStartDate.Text = currentDate;
        }
        else
        {
            Response.Redirect("AdminLogin.aspx");
        }
    }
    protected void ResetBtn_Click(object sender, EventArgs e)
    {
        tbxFirstName.Text = "";
        tbxLastName.Text = "";
        tbxAddress.Text = "";
        tbxPhone.Text = "";
        tbxStartDate.Text = currentDate;
        tbxLicense.Text = "";
        tbxPosition.Text = "";
        tbxCommission.Text = "";
        
    }
    protected void SaveBtn_Click(object sender, EventArgs e)
    {
        try
        {
            acc_id = int.Parse(Session["ACC_ID"].ToString());
            string firstName = tbxFirstName.Text;
            string lastName = tbxLastName.Text;
            string address = tbxAddress.Text;
            string phone = tbxPhone.Text;
            DateTime startDate = Convert.ToDateTime(tbxStartDate.Text);
            string license = tbxLicense.Text;
            string position = tbxPosition.Text;
            int commission = int.Parse(tbxCommission.Text);
            int id = 1;
            string checkPhone =  phone.Trim();
            
            EMPLOYEESDataAPI api = new EMPLOYEESDataAPI();
            DataTable dt = new DataTable();            
            dt = api.checkEmployee(acc_id, checkPhone);
            
            if (dt.Rows.Count >= 1)
            {
                lblMessage.ForeColor = System.Drawing.Color.Red;
                lblMessage.Text = "This employee already exists. Please check your input again!";
            }
            else
            {
                api.insertEMPLOYEES(ref id, acc_id, firstName, lastName, address, phone, startDate, license, position, commission);
                lblMessage.ForeColor = System.Drawing.Color.Blue;
                lblMessage.Text = "Insert Successfully!";
            }            
        }
        catch (Exception ex)
        {
            ex.ToString();
            lblMessage.ForeColor = System.Drawing.Color.Red;
            lblMessage.Text = "Please check your input again!";
        }
    }    
}
